package com.ewhale.feitengguest.ui.user.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.AuthInfoDto;
import com.ewhale.feitengguest.dto.BalanceDto;
import com.ewhale.feitengguest.dto.WalletLogDto;
import com.ewhale.feitengguest.presenter.user.mine.MyWalletPresenter;
import com.ewhale.feitengguest.ui.user.mine.adapter.BillAdapter;
import com.ewhale.feitengguest.ui.user.mine.adapter.WithdrawAdapter;
import com.ewhale.feitengguest.view.user.mine.MyWalletView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.HawkKey;
import com.simga.library.utils.StringUtil;
import com.simga.library.widget.BGButton;
import com.simga.library.widget.CoustomRefreshView;
import com.simga.library.widget.HintDialog;
import com.simga.library.widget.NGridView;
import com.simga.library.widget.NListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyWalletActivity extends MBaseActivity<MyWalletPresenter> implements MyWalletView, AdapterView.OnItemClickListener {
    @BindView(R.id.tv_balance)
    TextView mTvBalance;
    @BindView(R.id.gv_tixian)
    NGridView mGvTixian;
    @BindView(R.id.lv_zhangdan)
    NListView mLvZhangdan;
    @BindView(R.id.ref_layout)
    CoustomRefreshView mRefreshView;
    @BindView(R.id.btn_auth)
    BGButton mBtnAuth;
    @BindView(R.id.iv_back)
    ImageView ivBack;

    private WithdrawAdapter mWithdrawAdapter;
    private List<Integer> datas = new ArrayList<>();
    private List<WalletLogDto> mLogDtoList = new ArrayList<>();
    private BillAdapter mBillAdapter;
    private int pageNum = 1;
    private int authStatus;
    private HintDialog hintDialog;

    public static void open(MBaseActivity context) {
        Bundle bundle = new Bundle();
//        bundle.putInt("authStatus", authStatus);
        context.startActivity(bundle, MyWalletActivity.class);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        super.onGetBundle(bundle);
//        authStatus = bundle.getInt("authStatus");
    }

    @Override
    public void showAuthInfo(AuthInfoDto result) {
        authStatus = result.getStatus();
        Hawk.put(HawkKey.USER_AUTH_STATUS, result.getStatus());
        if (authStatus == 2) {
            mBtnAuth.setVisibility(View.INVISIBLE);
        } else {
            mBtnAuth.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected int getViewId() {
        return R.layout.activity_mywallet;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        datas.add(1);
        datas.add(10);
        datas.add(30);
        datas.add(50);
        datas.add(100);
        datas.add(500);
        mWithdrawAdapter = new WithdrawAdapter(mContext, datas);
        mGvTixian.setAdapter(mWithdrawAdapter);
        mBillAdapter = new BillAdapter(mContext, mLogDtoList);
        mLvZhangdan.setAdapter(mBillAdapter);
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    protected void initListener() {
        mGvTixian.setOnItemClickListener(this);
        mRefreshView.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNum = 1;
                presenter.loadWallteLog(pageNum);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pageNum++;
                presenter.loadWallteLog(pageNum);
            }
        });
        ivBack.setOnClickListener(v -> finish());
        mBtnAuth.setOnClickListener(v -> RNCertificationActivity.open(mContext));
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getId() == R.id.gv_tixian) {
            if (datas.get(position) > mWithdrawAdapter.balance) {
                return;
            }
            if (authStatus != 2) {
                hintDialog = new HintDialog(mContext, "提示", "提现需要实名认证，是否去实名认证？", new String[]{"取消", "确定"});
                hintDialog.setCallback(new HintDialog.Callback() {
                    @Override
                    public void callback() {
                        RNCertificationActivity.open(mContext);
                    }
                    @Override
                    public void cancle() {
                    }
                });
                hintDialog.show();
            } else {
                WithdrawActivity.open(mContext, datas.get(position));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.loadBalance();
        presenter.loadAuthInfo();
        pageNum = 1;
        presenter.loadWallteLog(pageNum);
    }

    @Override
    public void showBalance(BalanceDto result) {
        mTvBalance.setText(StringUtil.to2Decimal(StringUtil.to2Double(result.getBalance())));
        mWithdrawAdapter.balance = result.getBalance();
        mWithdrawAdapter.notifyDataSetChanged();
    }

    @Override
    public void showWalletLog(List<WalletLogDto> result, int data) {
        if (data == 1) {
            mLogDtoList.clear();
        }
        mLogDtoList.addAll(result);
        mBillAdapter.notifyDataSetChanged();
        onLoad(result.size());
    }

    public void onLoad(int size) {
        if (mRefreshView == null) {
            return;
        }
        mRefreshView.finishRefreshLoadingMore();
        if (size < 10) {
            mRefreshView.enableLoadMore(false);
        } else {
            mRefreshView.enableLoadMore(true);
        }
    }
}
